package com.jiangjiang.eshop.order.price.v1.calculator.impl;

import com.alibaba.fastjson.JSONObject;
import com.jiangjiang.eshop.order.dto.OrderItemDTO;
import com.jiangjiang.eshop.order.dto.PromotionActivityResult;
import com.jiangjiang.eshop.order.price.v1.calculator.PromotionActivityCalculator;
import com.jiangjiang.eshop.promotion.dto.PromotionActivityDTO;
import org.springframework.stereotype.Component;

/**
 * @Author Jianghaowen
 * @Date 2021/7/14
 * @Description: 单品促销活动的价格计算组件
 * @Version V1.0
 **/
@Component
public class DirectDiscountPromotionActivityCalculator implements PromotionActivityCalculator {
    /**
     * 单品促销活动的价格计算组件
     * @param item 订单条目
     * @param promotionActivity 促销活动
     * @return 价格计算结果
     * @throws Exception
     */
    @Override
    public PromotionActivityResult calculate(OrderItemDTO item, PromotionActivityDTO promotionActivity) throws Exception {
        double totalPrice = item.getPurchaseQuantity() * item.getPurchaseQuantity();
        //{“discountRate”: 0.7} 
        JSONObject role = JSONObject.parseObject(promotionActivity.getRule());
        Double discountRate = role.getDouble("discountRate");
        return PromotionActivityResult.builder()
                .discountAmount(totalPrice * (1.0 - discountRate))
                .build();

    }
}
